home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2901 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: lrz-muenchen.de!sun2!ua302aa
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Dereferencing void**
  5. Date: 24 Jan 1996 21:33:55 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4e68k3$8e@sparcserver.lrz-muenchen.de>
  9. References: <ragnaroek1996Jan22.114915.9372@news2.compulink.com>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. dirdata@idirect.com (Phill Moran) writes:
  13.  
  14. >If anyone has any sugestions about my current problem it woould be very 
  15. >helpfull.
  16. >I need to read in, at run time, a variable number of field that are also of 
  17. >varying types. My first thaought was:
  18.  
  19. >    void **field;
  20. >    *field    = new void * [NumberOfFields];
  21.  
  22. This is a bad idea in general, because "field" is an uninitialized pointer,
  23. and dereferencing it can lead to unexpected results.
  24.  
  25. Let us assume for the moment that you are talking about C, not about C++,
  26. so this is
  27.  
  28.     field = calloc(NumberOfFields, sizeof(void *));
  29.  
  30. >The advantage with this is that I can access the fields by index. The problem 
  31. >is how do I access them ? This snippet gives me errors
  32.  
  33. >    for (i=0;i<=NumberOfFields;i++)
  34. >        ((void *) (field))[i] = new int;
  35.  
  36. I am not sure what you are trying to do, but what is the problem 
  37. with something as simple as
  38.  
  39.     for (i = 0; i < NumberOfFields; i++)
  40.        field[i] = malloc(sizeof(int));
  41.  
  42. >So whar I need is an alyternate suggestion about data structures or the 
  43. >correct syntax.
  44.  
  45. If you want to use C++, try to look up pointers and operator new in
  46. a book about C++. If you want to use C, first try to get rid of
  47. the C++ features in your code. Then try to read a book on C that
  48. explains the _basic_ concept behind pointers.
  49.  
  50. Kurt
  51. --
  52. | Kurt Watzka                             Phone : +49-89-2180-6254
  53. | watzka@stat.uni-muenchen.de
  54. | ua302aa@sunmail.lrz-muenchen.de
  55.